<template>
  <div>
    <el-form ref="form" :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="建议人">
        <el-input v-model="formInline.keywords" placeholder="请输入……" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch('form')">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%" :row-key="'aa'">
      <el-table-column prop="id" label="ID" width="100"></el-table-column>
      <el-table-column prop="user_info.name" label="建议人" width="200"></el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="150"></el-table-column>
      <el-table-column prop="is_read" label="状态" width="150" :formatter="isReadFormat"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="see(scope.$index, scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @current-change ="handleCurrentChange"
        layout="total, prev, pager, next"
        :current-page="textData.current_page"
        :page-size="textData.per_page"
        :total="textData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {mapState} from 'vuex'

  export default {
    data () {
      return {
        formInline: {
          keywords: ''
        }
      }
    },
    computed: {
      ...mapState({
        tableData: state => state.opinion.opinionList,
        textData: state => state.opinion.pageData
      })
    },
    created () {
      this.opinionList()
    },
    methods: {
      // 获取意见反馈列表
      async opinionList () {
        let payload = {
          page: this.textData.current_page,
          listRows: this.textData.per_page,
          keywords: this.$route.query.keywords
        }
        await this.$store.dispatch('opinion/opinionList', payload)
      },
      // 处理检索条件
      onSearch (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$router.push({path: '/opinion/list', query: {keywords: this.formInline.keywords}})
            this.opinionList()
          }
        })
      },
      handleCurrentChange (val) {
        this.textData.current_page = val
        this.getSuggestionList()
      },
      // 查看意见反馈详情
      see (index, row) {
        if (row.is_read === 0) {
          let payload = {
            id: row.id
          }
          this.$store.dispatch('opinion/opinionRead', payload).then(res => {
            this.$store.commit('opinion/OPINION_SAVEDATA', row)
            this.$router.push('/opinion/info')
          })
        } else {
          this.$store.commit('opinion/OPINION_SAVEDATA', row)
          this.$router.push('/opinion/info')
        }
      },
      isReadFormat (value) {
        if (value.is_read === 0) {
          return '未读'
        } else if (value.is_read === 1) {
          return '已读'
        }
      }
    }
  }
</script>
